<template>
  <section v-loading="loading" class="cms-body">
    <div class="cms-list-header flex-between">
      <el-button type="primary" icon="el-icon-plus" @click="routerLink('/problemTopic/save','save','0')" v-perms="'/problemTopic/save'">添加
      </el-button>
      <div>
        <el-select v-model="params.isSlider" @change="goQuery()">
          <el-option label="轮播状态" value=""></el-option>
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>

        <el-select v-model="params.isDisplay" @change="goQuery()">
          <el-option label="展示状态" value=""></el-option>
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>

        <cms-input label="话题名称" v-model="params.problemTopicName"></cms-input>
        <el-button @click="goQuery()">查询</el-button>
      </div>
    </div>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="checkIds">
      <el-table-column type="selection" width="35" align="right"></el-table-column>
      <el-table-column prop="id" label="id" width="85" align="center"></el-table-column>
      <el-table-column prop="problemTopicName" label="话题名称" align="left"></el-table-column>
      <el-table-column prop="problemTopicImg" label="封面图" align="center">
        <template slot-scope="scope">
          <img-preivew :src="scope.row.problemTopicImg | imgPathFilter" :min-Width="50"></img-preivew>
        </template>
      </el-table-column>

      <el-table-column label="是否展示" prop="isDisplay" align="center" width="80">
        <div slot-scope="scope">
          <el-switch v-model="scope.row.isDisplay" active-color="#FFA500" inactive-color="#C0CCDA" :active-value="1"
                     :inactive-value="0" @change="changeState(scope.row.id,scope.row.isDisplay,1)">
          </el-switch>
        </div>
      </el-table-column>

      <el-table-column label="是否轮播" prop="isSlider" align="center" width="80">
        <div slot-scope="scope">
          <el-switch v-model="scope.row.isSlider" active-color="#FFA500" inactive-color="#C0CCDA" :active-value="1"
                     :inactive-value="0" @change="changeState(scope.row.id,scope.row.isSlider,2)">
          </el-switch>
        </div>
      </el-table-column>

      <el-table-column prop="problemTopicSort" label="排列顺序" align="center" width="80">
        <div slot-scope="scope">
          <el-input v-model="scope.row.problemTopicSort" type="number" class="w50"></el-input>
        </div>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <div slot-scope="scope">
          <cms-button type="edit" @click.native="routerLink('/problemTopic/update','update',scope.row.id)"
                      v-perms="'/problemTopic/update'"></cms-button>
          <cms-button type="delete" @click.native="batchDelete($api.problemTopicDelete,scope.row.id,'该操作会连话题问题下数据一同删除')"
                      v-perms="'/problemTopic/delete'"></cms-button>
        </div>
      </el-table-column>
    </el-table>

    <!-- 表格底部 -->
    <div class="cms-list-footer">
      <div class="cms-right">
        <el-button :disabled="disabled" @click="batchUpdate($api.problemTopicBatchUpdate)"
                   v-perms="'/problemTopic/batchUpdate'">保存排序
        </el-button>
        <el-button :disabled="disabled" @click="batchDelete($api.problemTopicDelete,ids,'该操作会连话题问题下数据一同删除')"
                   v-perms="'/problemTopic/delete'">批量删除
        </el-button>
      </div>
      <!-- cms分页组件 -->
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>
  </section>
</template>

<script>
  import listMixins from '@/mixins/list'

  export default {
    mixins: [listMixins],
    data() {
      return {
        params: {//只需要业务参数
          pageNo: 1,
          pageSize: 10,
          isSlider: '',
          isDisplay: ''
        }
      };
    },
    methods: {
      changeState(id, value, type) {
        let params = {}
        if (type == 1) {
          params = {problemTopicId: id, isDisplay: value}
        }
        if (type == 2) {
          params = {problemTopicId: id, isSlider: value}
        }
        this.$http.post(this.$api.problemTopicChangeState, params).then(res => {
          if (res.code == "200") {
            this.successMessage('操作成功');
            this.getTableData(this.params);
          }
        });
      },
      goQuery() {
        this.getTableData(this.params);
      },
      batchUpdate(url) { //保存排列循序
        let params = [];
        for (let i in this.checkedList) {
          let param = this.checkedList[i];
          param['problemTopicId'] = param.id;
          params.push(param)
        }
        params = JSON.stringify(params);
        this.$confirm('是否保存?', '提示', {type: "warning"}).then(mes => {
          this.$http.post(url, {params: params}).then(res => {
            if (res.code == "200") {
              this.successMessage('操作成功');
              this.getTableData(this.params);
            }
          });
        }).catch(error => {
          console.log(error)
        });
      }
    },
    created() {
      this.initTableData(this.$api.problemTopicPageList, this.params);
    }
  };
</script>

<style>

</style>
